1 MySQL用戶管理
1.1 查詢用戶
?
mysql> use mysql;
mysql> select * from user;
?
1.2 創(chuàng)建用戶
#主機名可使用%表示所有的主機
mysql> create user '用戶名'@'主機名' identified by '密碼';
?
1.3 刪除用戶
mysql> drop user 'mytest'@'localhost';
?
1.4 修改用戶密碼
#可選下面兩種寫法
mysql> update user set password = password('new_password') where user = 'mytest';
mysql> set password for 'mytest'@'localhost' = password('new_password');
?
2 MySQL 權限管理
2.1 查看用戶權限
#show grants for '用戶名'@'主機名'
mysql> show grants for 'mytest'@'%';
?
2.2 用戶授權
#grant 權限列表 on 數(shù)據(jù)庫名.表名 to '用戶名'@'主機名'; (多個權限需要使用逗號隔開)
mysql> grant all on *.* to 'mytest'@'%';
?
2.3 取消權限
#revoke 權限列表 on 數(shù)據(jù)庫名.表名 from '用戶名'@'主機名';
mysql> revoke select on company.account from 'mytest'@'%';
?
3 MySQL 四種連接方式
3.1 TCP/IP
TCP/IP連接方式是MySQL在任何平臺都提供的一種連接方式,通過TCP/IP連接建立一個網(wǎng)絡連接
mysql -uroot -h127.1 -p
?
遠程連接的客戶端連接的用戶有權限才可以被連接 , 可通過查詢 schema mysql 中的 user 表來得知用戶是否具有連接權限
3.2 Unix Socket
在Linux與Unix環(huán)境下,可使用Unix套接字連接,只能在MySQL客戶端與數(shù)據(jù)庫實例在同一臺服務器的情況下使用
mysql -u root -S /tmp/mysql.sock
?
-S 是 --socket 的簡寫形式 , 其值必須與服務器配置文件中指定路徑相同,此類連接性能優(yōu)于TCP/IP
3.3 Named Pipe
僅用于Windows下的連接,性能優(yōu)于TCP/IP 30%~50%
mysql -u username -p password --protocol=pipe [ --socket=mysql ]
?
3.4 Shared Memory
在4.1版本之后 , MySQL對Windows系統(tǒng)還提供了共享內存的連接方式
mysql --protocol=memory --shared-memory-base-name=mysql
?
4 MySQL 字符集設置
4.1 查看當前數(shù)據(jù)庫字符集
mysql> show variables like '%character%';
+--------------------------+-----------------------------------------------------+
| Variable_name ? ? ? ? ? ?| Value ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+--------------------------+-----------------------------------------------------+
| character_set_client ? ? | utf8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
| character_set_connection | utf8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
| character_set_database ? | utf8mb4 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_filesystem | binary ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
| character_set_results ? ?| utf8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
| character_set_server ? ? | utf8mb4 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_system ? ? | utf8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
| character_sets_dir ? ? ? | /usr/local/Percona-Server-5.7.19-17/share/charsets/ |
+--------------------------+-----------------------------------------------------+
8 rows in set (0.00 sec)
?
4.2 設置字符集
-
創(chuàng)庫時設置
mysql> create database db default character set=utf8;
?
創(chuàng)表時設置
create table tb(a char(2) not null primary key)default character set=utf8mb4;
?
創(chuàng)庫后設置
mysql> alter database db default character set gbk;
?
創(chuàng)表后設置
mysql> alter table tb convert to character set utf8;
字段級設置mysql> alter table tb modify 備注 text character set utf8mb4;
/*建立連接使用的編碼*/
mysql> set character_set_connection=utf8;
/*數(shù)據(jù)庫的編碼*/
mysql> set character_set_database=utf8;
/*結果集的編碼*/
mysql> set character_set_results=utf8;
/*數(shù)據(jù)庫服務器的編碼*/
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;
/*建立連接使用的編碼*/
mysql> set character_set_connection=utf8;
/*數(shù)據(jù)庫的編碼*/
mysql> set character_set_database=utf8;
/*結果集的編碼*/
mysql> set character_set_results=utf8;
/*數(shù)據(jù)庫服務器的編碼*/
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;
set指令僅臨時生效,永久生效需設置到my.cnf文件